
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
     
    <html xmlns="http://www.w3.org/1999/xhtml">
    
<head>   
   

    <script type="text/javascript" src="http://c.csdnimg.cn/pubfooter/js/tracking.js" charset="utf-8"></script>  

    <script type="text/javascript">
        var protocol = window.location.protocol;
        document.write('<script type="text/javascript" src="' + protocol + '//csdnimg.cn/pubfooter/js/repoAddr2.js?v=' + Math.random() + '"></' + 'script>');
    </script>

  
 <meta http-equiv="Cache-Control" content="no-siteapp" /><link rel="alternate" media="handheld" href="#" />

    <title>基础总结篇之五：BroadcastReceiver应用详解 - Scott Liu
        - 博客频道 - CSDN.NET</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta name="description" content="在Form1_Load事件中添加" />
    <script src="http://static.blog.csdn.net/scripts/jquery.js" type="text/javascript"></script>
      <script type="text/javascript" src="http://static.blog.csdn.net/scripts/jquery-version.js"></script>
    <script type="text/javascript" src="http://static.blog.csdn.net/scripts/ad.js?v=1.1"></script>
        <!--new top-->
               <link rel="stylesheet" href="http://c.csdnimg.cn/public/common/toolbar/css/index.css">        <!--new top-->
    
      <!-- ad begin -->
         <script language="javascript" type="text/javascript" src="http://ads.csdn.net/js/tracking.js"></script>
    <!-- ad end-->

    <link rel="Stylesheet" type="text/css" href="http://static.blog.csdn.net/skin/skin-blue/css/style.css?v=1.1" />
    <link id="RSSLink" title="RSS" type="application/rss+xml" rel="alternate" href="/liuhe688/rss/list" />
    <link rel="shortcut icon" href="http://c.csdnimg.cn/public/favicon.ico" />
    <link type="text/css" rel="stylesheet" href="http://static.blog.csdn.net/scripts/SyntaxHighlighter/styles/default.css" />
 


<script>
    var _hmt = _hmt || [];
    (function () {
        var hm = document.createElement("script");
        hm.src = "//hm.baidu.com/hm.js?6bcd52f51e9b3dce32bec4a3997715ac";
        var s = document.getElementsByTagName("script")[0];
        s.parentNode.insertBefore(hm, s);
    })();
</script>

</head>
<body>
    <!-- 广告位开始 -->
        <ins data-revive-zoneid="149" data-revive-id="8c38e720de1c90a6f6ff52f3f89c4d57"></ins>
    <!-- 广告位结束 -->

    
   
      <!--new top-->
    <script id="toolbar-tpl-scriptId" fixed="true" prod="blog" skin="black" src="http://c.csdnimg.cn/public/common/toolbar/js/html.js" type="text/javascript"></script>
     <!--new top-->
    <div id="container">
        <div id="header">
    <div class="header">
        <div id="blog_title">
            <h2>
                <a href="http://blog.csdn.net/liuhe688">Scott Liu</a></h2>
            <h3>Strive to be Extraordinary.</h3>
            <div class="clear">
            </div>
        </div>
        <div class="clear">
        </div>
        
     
    </div>
</div>
<div id="navigator">
    <div class="navigator_bg">
    </div>
    <div class="navigator">
        <ul>           
                <li id="btnContents"><a href="http://blog.csdn.net/liuhe688?viewmode=contents"><span onclick="_gaq.push(['_trackEvent','function', 'onclick', 'blog_articles_mulu'])">
                    <img src="http://static.blog.csdn.net/images/ico_list.gif">目录视图</span></a></li>
                <li id="btnView"><a href="http://blog.csdn.net/liuhe688?viewmode=list"><span onclick="_gaq.push(['_trackEvent','function', 'onclick', 'blog_articles_zhaiyao'])">
                    <img src="http://static.blog.csdn.net/images/ico_summary.gif">摘要视图</span></a></li>
                <li id="btnRss"><a href="http://blog.csdn.net/liuhe688/rss/list"><span onclick="_gaq.push(['_trackEvent','function', 'onclick', 'blog_articles_RSS'])">
                    <img src="http://static.blog.csdn.net/images/ico_rss.gif">订阅</span></a></li>                
            

            </ul>
    </div>
</div>
<script type="text/javascript">
    var username = "liuhe688";
    var _blogger = username;
    var blog_address = "http://blog.csdn.net/liuhe688";
    var static_host = "http://static.blog.csdn.net";
    var currentUserName = "";  
</script>

        <div id="body">
            <div id="main">
                <div class="main">
                        <div class="ad_class">
<div class="notice tracking-ad" data-mod='popu_3' > 
<a href="http://geek.csdn.net/news/detail/126445">
<font color=red>聚焦行业最佳实践，BDTC 2016完整议程公布
</font></a>

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<a href="http://huiyi.csdn.net/activity/product/goods_list?project_id=3287">
<font color=blue>微信小程序实战项目——点餐系统
</font></a>

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;

<a href="http://blog.csdn.net/turingbooks/article/details/53079366">
<font color=red>程序员11月书讯，评论得书啦 
</font></a>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;

<a href="http://geek.csdn.net/news/detail/110921">
<font color=blue>Get IT技能知识库，50个领域一键直达
</font></a>


</font></a>
</font></a>
</div>                        </div>
                        


<!-- 广告位开始 -->
<ins data-revive-zoneid="72" data-revive-id="8c38e720de1c90a6f6ff52f3f89c4d57"></ins>
<!-- 广告位结束 -->

  
<link href="http://static.blog.csdn.net/css/comment1.css" type="text/css" rel="stylesheet" />
<link href="http://static.blog.csdn.net/css/style1.css" type="text/css" rel="stylesheet" />
<script language='JavaScript' type='text/javascript' src='http://download.csdn.net/js/jquery.cookie.js'></script>
<script type="text/javascript" src="http://c.csdnimg.cn/rabbit/search-service/main.js"></script>
<link rel="stylesheet" href="http://static.blog.csdn.net/public/res-min/markdown_views.css?v=1.0" />
<link rel="stylesheet" href="http://static.blog.csdn.net/css/category.css?v=1.0" />
<script type="text/javascript" src="http://static.blog.csdn.net/public/res/bower-libs/MathJax/MathJax.js?config=TeX-AMS_HTML"></script>
<script type="text/javascript" src="http://static.blog.csdn.net/scripts/web-storage-cache.min.js"></script>
<script type="text/javascript" src="http://static.blog.csdn.net/scripts/replace.min.js"></script>




  <script type="text/ecmascript">
      window.quickReplyflag = true;
           
            var isBole = false;
            
          
    </script>
<div id="article_details" class="details">
    <div class="article_title">   
         <span class="ico ico_type_Original"></span>


    <h1>
        <span class="link_title"><a href="/liuhe688/article/details/6955668">
        基础总结篇之五：BroadcastReceiver应用详解            
        </a></span>
    </h1>
</div>

   

        <div class="article_manage clearfix">
        <div class="article_l">
            <span class="link_categories">
            标签：
              <a href='http://www.csdn.net/tag/android' target=_blank onclick="_gaq.push(['_trackEvent','function', 'onclick', 'blog_articles_tag']);">android</a><a href='http://www.csdn.net/tag/string' target=_blank onclick="_gaq.push(['_trackEvent','function', 'onclick', 'blog_articles_tag']);">string</a><a href='http://www.csdn.net/tag/service' target=_blank onclick="_gaq.push(['_trackEvent','function', 'onclick', 'blog_articles_tag']);">service</a><a href='http://www.csdn.net/tag/class' target=_blank onclick="_gaq.push(['_trackEvent','function', 'onclick', 'blog_articles_tag']);">class</a><a href='http://www.csdn.net/tag/action' target=_blank onclick="_gaq.push(['_trackEvent','function', 'onclick', 'blog_articles_tag']);">action</a><a href='http://www.csdn.net/tag/%e7%bd%91%e7%bb%9c' target=_blank onclick="_gaq.push(['_trackEvent','function', 'onclick', 'blog_articles_tag']);">网络</a>
            </span>
        </div>
        <div class="article_r">
            <span class="link_postdate">2011-11-11 17:48</span>
            <span class="link_view" title="阅读次数">168352人阅读</span>
            <span class="link_comments" title="评论次数"> <a href="#comments" onclick="_gaq.push(['_trackEvent','function', 'onclick', 'blog_articles_pinglun'])">评论</a>(159)</span>
            <span class="link_collect tracking-ad" data-mod="popu_171"> <a href="javascript:void(0);" onclick="javascript:collectArticle('基础总结篇之五：BroadcastReceiver应用详解','6955668');return false;" title="收藏">收藏</a></span>
             <span class="link_report"> <a href="#report" onclick="javascript:report(6955668,2);return false;" title="举报">举报</a></span>

        </div>
    </div>
    <div class="embody"  style="display:none" id="embody">
        <span class="embody_t">本文章已收录于：</span>
        <div class="embody_c" id="lib" value="{&quot;err&quot;:0,&quot;msg&quot;:&quot;ok&quot;,&quot;data&quot;:[]}"></div>
    </div>
    <style type="text/css">        
            .embody{
                padding:10px 10px 10px;
                margin:0 -20px;
                border-bottom:solid 1px #ededed;                
            }
            .embody_b{
                margin:0 ;
                padding:10px 0;
            }
            .embody .embody_t,.embody .embody_c{
                display: inline-block;
                margin-right:10px;
            }
            .embody_t{
                font-size: 12px;
                color:#999;
            }
            .embody_c{
                font-size: 12px;
            }
            .embody_c img,.embody_c em{
                display: inline-block;
                vertical-align: middle;               
            }
             .embody_c img{               
                width:30px;
                height:30px;
            }
            .embody_c em{
                margin: 0 20px 0 10px;
                color:#333;
                font-style: normal;
            }
    </style>
    <script  type="text/javascript">
        $(function () {
            try
            {
                var lib = eval("("+$("#lib").attr("value")+")");
                var html = "";
                if (lib.err == 0) {
                    $.each(lib.data, function (i) {
                        var obj = lib.data[i];
                        //html += '<img src="' + obj.logo + '"/>' + obj.name + "&nbsp;&nbsp;";
                        html += ' <a href="' + obj.url + '" target="_blank">';
                        html += ' <img src="' + obj.logo + '">';
                        html += ' <em><b>' + obj.name + '</b></em>';
                        html += ' </a>';
                    });
                    if (html != "") {
                        setTimeout(function () {
                            $("#lib").html(html);                      
                            $("#embody").show();
                        }, 100);
                    }
                }      
            } catch (err)
            { }
            
        });
    </script>
      <div class="category clearfix">
        <div class="category_l">
           <img src="http://static.blog.csdn.net/images/category_icon.jpg">
            <span>分类：</span>
        </div>
        <div class="category_r">
                    <label  onclick="GetCategoryArticles('790276','liuhe688','top','6955668');">
                        <span onclick="_gaq.push(['_trackEvent','function', 'onclick', 'blog_articles_fenlei']);">Android<em>（31）</em></span>
                      <img class="arrow-down" src="http://static.blog.csdn.net/images/arrow_triangle _down.jpg" style="display:inline;">
                      <img class="arrow-up" src="http://static.blog.csdn.net/images/arrow_triangle_up.jpg" style="display:none;">
                        <div class="subItem">
                            <div class="subItem_t"><a  href="http://blog.csdn.net/liuhe688/article/category/790276"  target="_blank">作者同类文章</a><i class="J_close">X</i></div>
                            <ul class="subItem_l" id="top_790276">                            
                            </ul>
                        </div>
                    </label>                    
        </div>
    </div>
    <script   type="text/javascript" src="http://static.blog.csdn.net/scripts/category.js"></script>  
        <div   class="bog_copyright">         
            <p  class="copyright_p" >版权声明：本文为博主原创文章，转载请注明出处。</p>
        </div>

  

  
  
     

<div id="article_content" class="article_content">

<p>問渠那得清如許？為有源頭活水來。南宋.朱熹《觀書有感》</p>
<p>据说程序员是最爱学习的群体，IT男都知道，这个行业日新月异，必须不断地学习新知识，不断地为自己注入新鲜的血液，才能使自己跟上技术的步伐。<br>
</p>
<p>今天我们来讲一下Android中BroadcastReceiver的相关知识。</p>
<p>BroadcastReceiver也就是“广播接收者”的意思，顾名思义，它就是用来接收来自系统和应用中的广播。</p>
<p>在Android系统中，广播体现在方方面面，例如当开机完成后系统会产生一条广播，接收到这条广播就能实现开机启动服务的功能；当网络状态改变时系统会产生一条广播，接收到这条广播就能及时地做出提示和保存数据等操作；当电池电量改变时，系统会产生一条广播，接收到这条广播就能在电量低时告知用户及时保存进度，等等。</p>
<p>Android中的广播机制设计的非常出色，很多事情原本需要开发者亲自操作的，现在只需等待广播告知自己就可以了，大大减少了开发的工作量和开发周期。而作为应用开发者，就需要数练掌握Android系统提供的一个开发利器，那就是BroadcastReceiver。下面我们就对BroadcastReceiver逐一地分析和演练，了解和掌握它的各种功能和用法。</p>
<p>首先，我们来演示一下创建一个BroadcastReceiver，并让这个BroadcastReceiver能够根据我们的需要来运行。</p>
<p>要创建自己的BroadcastReceiver对象，我们需要继承android.content.BroadcastReceiver，并实现其onReceive方法。下面我们就创建一个名为MyReceiver广播接收者：</p>
<p></p>
<pre name="code" class="java">package com.scott.receiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

public class MyReceiver extends BroadcastReceiver {
	
	private static final String TAG = &quot;MyReceiver&quot;;
	
	@Override
	public void onReceive(Context context, Intent intent) {
		String msg = intent.getStringExtra(&quot;msg&quot;);
		Log.i(TAG, msg);
	}

}
</pre>
<p>在onReceive方法内，我们可以获取随广播而来的Intent中的数据，这非常重要，就像无线电一样，包含很多有用的信息。</p>
<p>在创建完我们的BroadcastReceiver之后，还不能够使它进入工作状态，我们需要为它注册一个指定的广播地址。没有注册广播地址的BroadcastReceiver就像一个缺少选台按钮的收音机，虽然功能俱备，但也无法收到电台的信号。下面我们就来介绍一下如何为BroadcastReceiver注册广播地址。</p>
<p><strong>静态注册</strong><br>
</p>
<p>静态注册是在AndroidManifest.xml文件中配置的，我们就来为MyReceiver注册一个广播地址：</p>
<p></p>
<pre name="code" class="html">&lt;receiver android:name=&quot;.MyReceiver&quot;&gt;
        	&lt;intent-filter&gt;
        		&lt;action android:name=&quot;android.intent.action.MY_BROADCAST&quot;/&gt;
        		&lt;category android:name=&quot;android.intent.category.DEFAULT&quot; /&gt;
        	&lt;/intent-filter&gt;
        &lt;/receiver&gt;</pre>
<p>配置了以上信息之后，只要是android.intent.action.MY_BROADCAST这个地址的广播，MyReceiver都能够接收的到。注意，这种方式的注册是常驻型的，也就是说当应用关闭后，如果有广播信息传来，MyReceiver也会被系统调用而自动运行。</p>
<p><strong>动态注册</strong></p>
<p>动态注册需要在代码中动态的指定广播地址并注册，通常我们是在Activity或Service注册一个广播，下面我们就来看一下注册的代码：</p>
<p></p>
<pre name="code" class="java">MyReceiver receiver = new MyReceiver();
        
IntentFilter filter = new IntentFilter();
filter.addAction(&quot;android.intent.action.MY_BROADCAST&quot;);
        
registerReceiver(receiver, filter);</pre>注意，registerReceiver是android.content.ContextWrapper类中的方法，Activity和Service都继承了ContextWrapper，所以可以直接调用。在实际应用中，我们在Activity或Service中注册了一个BroadcastReceiver，当这个Activity或Service被销毁时如果没有解除注册，系统会报一个异常，提示我们是否忘记解除注册了。所以，记得在特定的地方执行解除注册操作：
<p></p>
<p></p>
<pre name="code" class="java">@Override
protected void onDestroy() {
    super.onDestroy();
    unregisterReceiver(receiver);
}</pre>
<p></p>
<p>执行这样行代码就可以解决问题了。注意，这种注册方式与静态注册相反，不是常驻型的，也就是说广播会跟随程序的生命周期。</p>
<p>我们可以根据以上任意一种方法完成注册，当注册完成之后，这个接收者就可以正常工作了。我们可以用以下方式向其发送一条广播：</p>
<p></p>
<pre name="code" class="java">    public void send(View view) {
    	Intent intent = new Intent(&quot;android.intent.action.MY_BROADCAST&quot;);
    	intent.putExtra(&quot;msg&quot;, &quot;hello receiver.&quot;);
    	sendBroadcast(intent);
    }</pre>
<p></p>
<p>注意，sendBroadcast也是android.content.ContextWrapper类中的方法，它可以将一个指定地址和参数信息的Intent对象以广播的形式发送出去。</p>
<p>点击发送按钮，执行send方法，控制台打印如下：</p>
<p><img src="http://hi.csdn.net/attachment/201111/10/0_13209080389QEC.gif" alt=""><br>
</p>
<p>看到这样的打印信息，表明我们的广播已经发出去了，并且被MyReceiver准确无误的接收到了。</p>
<p>上面的例子只是一个接收者来接收广播，如果有多个接收者都注册了相同的广播地址，又会是什么情况呢，能同时接收到同一条广播吗，相互之间会不会有干扰呢？这就涉及到普通广播和有序广播的概念了。</p>
<p><strong>普通广播（Normal Broadcast）</strong></p>
<p>普通广播对于多个接收者来说是完全异步的，通常每个接收者都无需等待即可以接收到广播，接收者相互之间不会有影响。对于这种广播，接收者无法终止广播，即无法阻止其他接收者的接收动作。</p>
<p>为了验证以上论断，我们新建三个BroadcastReceiver，演示一下这个过程，FirstReceiver、SecondReceiver和ThirdReceiver的代码如下：</p>
<p></p>
<pre name="code" class="java">package com.scott.receiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

public class FirstReceiver extends BroadcastReceiver {
	
	private static final String TAG = &quot;NormalBroadcast&quot;;
	
	@Override
	public void onReceive(Context context, Intent intent) {
		String msg = intent.getStringExtra(&quot;msg&quot;);
		Log.i(TAG, &quot;FirstReceiver: &quot; + msg);
	}

}
</pre><pre name="code" class="java">public class SecondReceiver extends BroadcastReceiver {
	
	private static final String TAG = &quot;NormalBroadcast&quot;;
	
	@Override
	public void onReceive(Context context, Intent intent) {
		String msg = intent.getStringExtra(&quot;msg&quot;);
		Log.i(TAG, &quot;SecondReceiver: &quot; + msg);
	}

}
</pre><pre name="code" class="java">public class ThirdReceiver extends BroadcastReceiver {
	
	private static final String TAG = &quot;NormalBroadcast&quot;;
	
	@Override
	public void onReceive(Context context, Intent intent) {
		String msg = intent.getStringExtra(&quot;msg&quot;);
		Log.i(TAG, &quot;ThirdReceiver: &quot; + msg);
	}

}
</pre>然后再次点击发送按钮，发送一条广播，控制台打印如下：
<p></p>
<p><img src="http://hi.csdn.net/attachment/201111/10/0_13209098830caO.gif" alt=""><br>
</p>
<p>看来这三个接收者都接收到这条广播了，我们稍微修改一下三个接收者，在onReceive方法的最后一行添加以下代码，试图终止广播：</p>
<p></p>
<pre name="code" class="java">abortBroadcast();</pre>再次点击发送按钮，我们会发现，控制台中三个接收者仍然都打印了自己的日志，表明接收者并不能终止广播。
<p></p>
<p><strong>有序广播（Ordered Broadcast）</strong></p>
<p>有序广播比较特殊，它每次只发送到优先级较高的接收者那里，然后由优先级高的接受者再传播到优先级低的接收者那里，优先级高的接收者有能力终止这个广播。<br>
</p>
<p>为了演示有序广播的流程，我们修改一下上面三个接收者的代码，如下：</p>
<p></p>
<pre name="code" class="java">package com.scott.receiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;

public class FirstReceiver extends BroadcastReceiver {
	
	private static final String TAG = &quot;OrderedBroadcast&quot;;
	
	@Override
	public void onReceive(Context context, Intent intent) {
		String msg = intent.getStringExtra(&quot;msg&quot;);
		Log.i(TAG, &quot;FirstReceiver: &quot; + msg);
		
		Bundle bundle = new Bundle();
		bundle.putString(&quot;msg&quot;, msg + &quot;@FirstReceiver&quot;);
		setResultExtras(bundle);
	}

}
</pre><pre name="code" class="java">public class SecondReceiver extends BroadcastReceiver {
	
	private static final String TAG = &quot;OrderedBroadcast&quot;;
	
	@Override
	public void onReceive(Context context, Intent intent) {
		String msg = getResultExtras(true).getString(&quot;msg&quot;);
		Log.i(TAG, &quot;SecondReceiver: &quot; + msg);
		
		Bundle bundle = new Bundle();
		bundle.putString(&quot;msg&quot;, msg + &quot;@SecondReceiver&quot;);
		setResultExtras(bundle);
	}

}</pre><pre name="code" class="java">public class ThirdReceiver extends BroadcastReceiver {
	
	private static final String TAG = &quot;OrderedBroadcast&quot;;
	
	@Override
	public void onReceive(Context context, Intent intent) {
		String msg = getResultExtras(true).getString(&quot;msg&quot;);
		Log.i(TAG, &quot;ThirdReceiver: &quot; + msg);
	}

}</pre>我们注意到，在FirstReceiver和SecondReceiver中最后都使用了setResultExtras方法将一个Bundle对象设置为结果集对象，传递到下一个接收者那里，这样以来，优先级低的接收者可以用getResultExtras获取到最新的经过处理的信息集合。
<p></p>
<p>代码改完之后，我们需要为三个接收者注册广播地址，我们修改一下AndroidMainfest.xml文件：</p>
<p></p>
<pre name="code" class="html">        &lt;receiver android:name=&quot;.FirstReceiver&quot;&gt;
        	&lt;intent-filter android:priority=&quot;1000&quot;&gt;
        		&lt;action android:name=&quot;android.intent.action.MY_BROADCAST&quot;/&gt;
        		&lt;category android:name=&quot;android.intent.category.DEFAULT&quot; /&gt;
        	&lt;/intent-filter&gt;
        &lt;/receiver&gt;
        &lt;receiver android:name=&quot;.SecondReceiver&quot;&gt;
        	&lt;intent-filter android:priority=&quot;999&quot;&gt;
        		&lt;action android:name=&quot;android.intent.action.MY_BROADCAST&quot;/&gt;
        		&lt;category android:name=&quot;android.intent.category.DEFAULT&quot; /&gt;
        	&lt;/intent-filter&gt;
        &lt;/receiver&gt;
        &lt;receiver android:name=&quot;.ThirdReceiver&quot;&gt;
        	&lt;intent-filter android:priority=&quot;998&quot;&gt;
        		&lt;action android:name=&quot;android.intent.action.MY_BROADCAST&quot;/&gt;
        		&lt;category android:name=&quot;android.intent.category.DEFAULT&quot; /&gt;
        	&lt;/intent-filter&gt;
        &lt;/receiver&gt;</pre>我们看到，现在这三个接收者的&lt;intent-filter&gt;多了一个android:priority属性，并且依次减小。这个属性的范围在-1000到1000，数&#20540;越大，优先级越高。
<p></p>
<p>现在，我们需要修改一下发送广播的代码，如下：</p>
<p></p>
<pre name="code" class="java">    public void send(View view) {
    	Intent intent = new Intent(&quot;android.intent.action.MY_BROADCAST&quot;);
    	intent.putExtra(&quot;msg&quot;, &quot;hello receiver.&quot;);
    	sendOrderedBroadcast(intent, &quot;scott.permission.MY_BROADCAST_PERMISSION&quot;);
    }</pre>注意，使用sendOrderedBroadcast方法发送有序广播时，需要一个权限参数，如果为null则表示不要求接收者声明指定的权限，如果不为null，则表示接收者若要接收此广播，需声明指定权限。这样做是从安全角度考虑的，例如系统的短信就是有序广播的形式，一个应用可能是具有拦截垃圾短信的功能，当短信到来时它可以先接受到短信广播，必要时终止广播传递，这样的软件就必须声明接收短信的权限。
<p></p>
<p>所以我们在AndroidMainfest.xml中定义一个权限：</p>
<p></p>
<pre name="code" class="html">    &lt;permission android:protectionLevel=&quot;normal&quot;
    			android:name=&quot;scott.permission.MY_BROADCAST_PERMISSION&quot; /&gt;</pre>然后声明使用了此权限：
<p></p>
<p></p>
<pre name="code" class="html">&lt;uses-permission android:name=&quot;scott.permission.MY_BROADCAST_PERMISSION&quot; /&gt;</pre>
<p>关于这部分如果有不明白的地方可以参考我之前写过的一篇文章：<a href="http://blog.csdn.net/liuhe688/article/details/6417983" title="Android声明和使用权限" target="_blank">Android声明和使用权限</a></p>
<p>然后我们点击发送按钮发送一条广播，控制台打印如下：</p>
<p><img src="http://hi.csdn.net/attachment/201111/10/0_1320913997FkKj.gif" alt=""></p>
<p>我们看到接收是按照顺序的，第一个和第二个都在结果集中加入了自己的标记，并且向优先级低的接收者传递下去。</p>
<p>既然是顺序传递，试着终止这种传递，看一看效果如何，我们修改FirstReceiver的代码，在onReceive的最后一行添加以下代码：</p>
<p></p>
<pre name="code" class="java">abortBroadcast();</pre>然后再次运行程序，控制台打印如下：
<p></p>
<p><img src="http://hi.csdn.net/attachment/201111/10/0_1320914377lOyl.gif" alt=""><br>
</p>
<p>此次，只有第一个接收者执行了，其它两个都没能执行，因为广播被第一个接收者终止了。</p>
<p>上面就是BroadcastReceiver的介绍，下面我将会举几个常见的例子加深一下大家对广播的理解和应用：</p>
<p><strong>1.开机启动服务</strong></p>
<p>我们经常会有这样的应用场合，比如消息推送服务，需要实现开机启动的功能。要实现这个功能，我们就可以订阅系统“启动完成”这条广播，接收到这条广播后我们就可以启动自己的服务了。我们来看一下BootCompleteReceiver和MsgPushService的具体实现：</p>
<p></p>
<pre name="code" class="java">package com.scott.receiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

public class BootCompleteReceiver extends BroadcastReceiver {
	
	private static final String TAG = &quot;BootCompleteReceiver&quot;;
	
	@Override
	public void onReceive(Context context, Intent intent) {
		Intent service = new Intent(context, MsgPushService.class);
		context.startService(service);
		Log.i(TAG, &quot;Boot Complete. Starting MsgPushService...&quot;);
	}

}
</pre><pre name="code" class="java">package com.scott.receiver;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;

public class MsgPushService extends Service {

	private static final String TAG = &quot;MsgPushService&quot;;
	
	@Override
	public void onCreate() {
		super.onCreate();
		Log.i(TAG, &quot;onCreate called.&quot;);
	}
	
	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {
		Log.i(TAG, &quot;onStartCommand called.&quot;);
		return super.onStartCommand(intent, flags, startId);
	}

	@Override
	public IBinder onBind(Intent arg0) {
		return null;
	}
}
</pre>然后我们需要在AndroidManifest.xml中配置相关信息：
<p></p>
<p></p>
<pre name="code" class="html">        &lt;!-- 开机广播接受者 --&gt;
        &lt;receiver android:name=&quot;.BootCompleteReceiver&quot;&gt;
        	&lt;intent-filter&gt;
        		&lt;!-- 注册开机广播地址--&gt;
        		&lt;action android:name=&quot;android.intent.action.BOOT_COMPLETED&quot;/&gt;
        		&lt;category android:name=&quot;android.intent.category.DEFAULT&quot; /&gt;
        	&lt;/intent-filter&gt;
        &lt;/receiver&gt;
        &lt;!-- 消息推送服务 --&gt;
        &lt;service android:name=&quot;.MsgPushService&quot;/&gt;</pre>我们看到BootCompleteReceiver注册了“android.intent.action.BOOT_COMPLETED”这个开机广播地址，从安全角度考虑，系统要求必须声明接收开机启动广播的权限，于是我们再声明使用下面的权限：
<p></p>
<p></p>
<pre name="code" class="html">&lt;uses-permission android:name=&quot;android.permission.RECEIVE_BOOT_COMPLETED&quot; /&gt;</pre>经过上面的几个步骤之后，我们就完成了开机启动的功能，将应用运行在模拟器上，然后重启模拟器，控制台打印如下：
<p></p>
<p><img src="http://hi.csdn.net/attachment/201111/11/0_1320971403u3G5.gif" alt=""><br>
</p>
<p><img src="http://hi.csdn.net/attachment/201111/11/0_1320971416MKgc.gif" alt=""><br>
</p>
<p>如果我们查看已运行的服务就会发现，MsgPushService已经运行起来了。</p>
<p><strong>2.网络状态变化</strong></p>
<p>在某些场合，比如用户浏览网络信息时，网络突然断开，我们要及时地提醒用户网络已断开。要实现这个功能，我们可以接收网络状态改变这样一条广播，当由连接状态变为断开状态时，系统就会发送一条广播，我们接收到之后，再通过网络的状态做出相应的操作。下面就来实现一下这个功能：</p>
<p></p>
<pre name="code" class="java">package com.scott.receiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.util.Log;
import android.widget.Toast;

public class NetworkStateReceiver extends BroadcastReceiver {
	
	private static final String TAG = &quot;NetworkStateReceiver&quot;;
	
	@Override
	public void onReceive(Context context, Intent intent) {
		Log.i(TAG, &quot;network state changed.&quot;);
		if (!isNetworkAvailable(context)) {
			Toast.makeText(context, &quot;network disconnected!&quot;, 0).show();
		}
	}
	
	/**
	 * 网络是否可用
	 * 
	 * @param context
	 * @return
	 */
	public static boolean isNetworkAvailable(Context context) {
		ConnectivityManager mgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo[] info = mgr.getAllNetworkInfo();
		if (info != null) {
			for (int i = 0; i &lt; info.length; i++) {
				if (info[i].getState() == NetworkInfo.State.CONNECTED) {
					return true;
				}
			}
		}
		return false;
	}

}
</pre>再注册一下这个接收者的信息：
<p></p>
<p></p>
<pre name="code" class="html">        &lt;receiver android:name=&quot;.NetworkStateReceiver&quot;&gt;
        	&lt;intent-filter&gt;
        		&lt;action android:name=&quot;android.net.conn.CONNECTIVITY_CHANGE&quot;/&gt;
        		&lt;category android:name=&quot;android.intent.category.DEFAULT&quot; /&gt;
        	&lt;/intent-filter&gt;
        &lt;/receiver&gt;</pre>因为在isNetworkAvailable方法中我们使用到了网络状态相关的API，所以需要声明相关的权限才行，下面就是对应的权限声明：
<p></p>
<p></p>
<pre name="code" class="html">&lt;uses-permission android:name=&quot;android.permission.ACCESS_NETWORK_STATE&quot;/&gt;</pre>我们可以测试一下，比如关闭WiFi，看看有什么效果。
<p></p>
<p><strong>3.电量变化</strong></p>
<p>如果我们阅读软件，可能是全屏阅读，这个时候用户就看不到剩余的电量，我们就可以为他们提供电量的信息。要想做到这一点，我们需要接收一条电量变化的广播，然后获取百分比信息，这听上去挺简单的，我们就来实现以下：</p>
<p></p>
<pre name="code" class="java">package com.scott.receiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.BatteryManager;
import android.util.Log;

public class BatteryChangedReceiver extends BroadcastReceiver {

	private static final String TAG = &quot;BatteryChangedReceiver&quot;;
	
	@Override
	public void onReceive(Context context, Intent intent) {
		int currLevel = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, 0);	//当前电量
		int total = intent.getIntExtra(BatteryManager.EXTRA_SCALE, 1);		//总电量
		int percent = currLevel * 100 / total;
		Log.i(TAG, &quot;battery: &quot; + percent + &quot;%&quot;);
	}

}
</pre>然后再注册一下广播接地址信息就可以了：
<p></p>
<p></p>
<pre name="code" class="html">        &lt;receiver android:name=&quot;.BatteryChangedReceiver&quot;&gt;
        	&lt;intent-filter&gt;
        		&lt;action android:name=&quot;android.intent.action.BATTERY_CHANGED&quot;/&gt;
        		&lt;category android:name=&quot;android.intent.category.DEFAULT&quot; /&gt;
        	&lt;/intent-filter&gt;
        &lt;/receiver&gt;</pre>
<p>当然，有些时候我们是要立即获取电量的，而不是等电量变化的广播，比如当阅读软件打开时立即显示出电池电量。我们可以按以下方式获取：</p>
<p></p>
<pre name="code" class="java">Intent batteryIntent = getApplicationContext().registerReceiver(null,
        new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
int currLevel = batteryIntent.getIntExtra(BatteryManager.EXTRA_LEVEL, 0);
int total = batteryIntent.getIntExtra(BatteryManager.EXTRA_SCALE, 1);
int percent = currLevel * 100 / total;
Log.i(&quot;battery&quot;, &quot;battery: &quot; + percent + &quot;%&quot;);</pre><br>
<p></p>
   
</div>




<!-- Baidu Button BEGIN -->




<div class="bdsharebuttonbox tracking-ad" style="float: right;" data-mod="popu_172">
<a href="#" class="bds_more" data-cmd="more" style="background-position:0 0 !important; background-image: url(http://bdimg.share.baidu.com/static/api/img/share/icons_0_16.png?v=d754dcc0.png) !important"></a>
<a href="#" class="bds_qzone" data-cmd="qzone" title="分享到QQ空间"  style="background-position:0 -52px !important"></a>
<a href="#" class="bds_tsina" data-cmd="tsina" title="分享到新浪微博"style="background-position:0 -104px !important"></a>
<a href="#" class="bds_tqq" data-cmd="tqq" title="分享到腾讯微博"style="background-position:0 -260px !important"></a>
<a href="#" class="bds_renren" data-cmd="renren" title="分享到人人网"style="background-position:0 -208px !important"></a>
<a href="#" class="bds_weixin" data-cmd="weixin" title="分享到微信"style="background-position:0 -1612px !important" ></a>
</div>
<script>window._bd_share_config = { "common": { "bdSnsKey": {}, "bdText": "", "bdMini": "1", "bdMiniList": false, "bdPic": "", "bdStyle": "0", "bdSize": "16" }, "share": {} }; with (document) 0[(getElementsByTagName('head')[0] || body).appendChild(createElement('script')).src = 'http://bdimg.share.baidu.com/static/api/js/share.js?v=89860593.js?cdnversion=' + ~(-new Date() / 36e5)];</script>
<!-- Baidu Button END -->

   <link rel="stylesheet" href="http://static.blog.csdn.net/css/blog_detail.css" />

    
<!--172.16.140.13-->

<!-- Baidu Button BEGIN -->
<script type="text/javascript" id="bdshare_js" data="type=tools&amp;uid=1536434" ></script>
<script type="text/javascript" id="bdshell_js"></script>
<script type="text/javascript">
    document.getElementById("bdshell_js").src = "http://bdimg.share.baidu.com/static/js/shell_v2.js?cdnversion=" + Math.ceil(new Date()/3600000)
</script>
<!-- Baidu Button END -->

 

        <div id="digg" ArticleId="6955668" >
            <dl id="btnDigg" class="digg digg_disable"  onclick="btndigga();">
               
                 <dt>顶</dt>
                <dd>49</dd>
            </dl>
           
              
            <dl id="btnBury" class="digg digg_disable"  onclick="btnburya();">
              
                  <dt>踩</dt>
                <dd>0</dd>               
            </dl>
            
        </div>
     <div class="tracking-ad" data-mod="popu_222"><a href="javascript:void(0);" >&nbsp;</a>   </div>
    <div class="tracking-ad" data-mod="popu_223"> <a href="javascript:void(0);" >&nbsp;</a></div>
    <script type="text/javascript">
                function btndigga() {
                    $(".tracking-ad[data-mod='popu_222'] a").click();
                }
                function btnburya() {
                    $(".tracking-ad[data-mod='popu_223'] a").click();
                }
            </script>

   <ul class="article_next_prev">
                <li class="prev_article"><span  onclick="_gaq.push(['_trackEvent','function', 'onclick', 'blog_articles_shangyipian']);location.href='/liuhe688/article/details/6917905';">上一篇</span><a href="/liuhe688/article/details/6917905" onclick="_gaq.push(['_trackEvent','function', 'onclick', 'blog_articles_shangyipian'])">Windows下配置Ruby On Rails</a></li>
                <li class="next_article"><span onclick="_gaq.push(['_trackEvent','function', 'onclick', 'blog_articles_xiayipian']);location.href='/liuhe688/article/details/7006556';">下一篇</span><a href="/liuhe688/article/details/7006556" onclick="_gaq.push(['_trackEvent','function', 'onclick', 'blog_articles_xiayipian'])">基础总结篇之六：ContentProvider之读写联系人</a></li>
    </ul>

    <div style="clear:both; height:10px;"></div>


        <div class="similar_article"  style="display:none">
                <h4>我的同类文章</h4>
                <div class="similar_c"style="margin:20px 0px 0px 0px">
                    <div class="similar_c_t">
                                <label class="similar_cur">
                                    <span  style="cursor:pointer"  onclick="GetCategoryArticles('790276','liuhe688','foot','6955668');">Android<em>（31）</em></span>
                                </label>
                    </div>
                   
                    <div class="similar_wrap tracking-ad" data-mod="popu_141"  style="max-height:195px;">
                        <a href="http://blog.csdn.net" style="display:none">http://blog.csdn.net</a>
                        <ul class="similar_list fl">                          
                        </ul>

                        <ul class="similar_list fr">                           
                        </ul>
                    </div>
                </div>
            </div>    
    <script  type="text/javascript">
        $(function () {
            GetCategoryArticles('790276', 'liuhe688','foot','6955668');
        });
    </script>
      
</div>
<div id="suggest"></div>
         <script  language="javascript" type='text/javascript'>     
             $(function(){
                 $.get("/liuhe688/svc/GetSuggestContent/6955668",function(data){
                     $("#suggest").html(data);
                 });     
             });             
         </script>  


<style>
.blog-ass-articl dd {
color: #369;
width: 99%; /*修改行*/
float: left;
overflow: hidden;
font: normal normal 12px/23px "SimSun";
height: 23px;
margin: 0;
padding: 0 0 0 10px;
margin-right: 30px;
background: url(http://static.blog.csdn.net/skin/default/images/blog-dot-red3.gif) no-repeat 0 10px;
}
</style>

 <link rel="stylesheet" href="http://static.blog.csdn.net/css/replace.css"/>
<div id="relate" data-mod="popu_218"  class="tracking-ad">
        <div class="relate_t">
            <h3><span>参考知识库</span></h3>
        </div>
        <div class="relate_c">
        </div>
</div>
 

<dl class="blog-ass-articl" id="res-relatived" > 
    <div class="embody embody_b" id="libkeyparent"  style="display:none">
            <span class="embody_t">更多资料请参考：</span>
            <div class="embody_c" id="libkey"></div>
    </div>


     <dt><span>猜你在找</span></dt>    


   


    <div id="adCollege" style="width: 42%;float: left;"> 
        <script src="http://csdnimg.cn/jobreco/job_reco.js" type="text/javascript"></script> 
        <script type="text/javascript">
            csdn.position.showEdu({
                sourceType: "blog",
                searchType: "detail",
                searchKey: "6955668",
                username: "",
                recordcount: "5",
                containerId: "adCollege" //容器DIV的id。 
            });
        </script> 
    </div>  

    
     <div id="res"  data-mod="popu_36"  class="tracking-ad" style="width: 42%;float: left;margin-right: 30px;"></div>
   
</dl>


<script type="text/javascript">
    $(function () {
        setTimeout(function () {
            var searchtitletags = '基础总结篇之五：BroadcastReceiver应用详解' + ',' + $("#tags").html();
            searchService({
                index: 'blog',
                query: searchtitletags,
                from: 5,
                size: 5,
                appendTo: '#res',
                url: 'recommend',
                his: 2,
                client: "blog_cf_enhance",
                tmpl: '<dd style="background:url(http://static.blog.csdn.net/skin/default/images/blog-dot-red3.gif) no-repeat 0 10px;"><a href="#{ url }" title="#{ title }" strategy="#{ strategy }">#{ title }</a></dd>'
            });
        }, 500);
    });    

 </script>  

    <div id="ad_cen">        
                    <script type="text/javascript">
                        /*博客内容页下方Banner-728*90，创建于2014-7-3*/
                        var cpro_id = "u1607657";
                    </script>
                    <script type="text/javascript" src="http://cpro.baidustatic.com/cpro/ui/c.js"></script>
    </div>  

<div class="comment_class">
    <div id="comment_title" class="panel_head">
        <span class="see_comment">查看评论</span><a name="comments"></a></div>
    <div id="comment_list">
    </div>
    <div id="comment_bar">
    </div>
    <div id="comment_form">
    </div>
    <div class="announce">
        * 以上用户言论只代表其个人观点，不代表CSDN网站的观点或立场<a name="reply"></a><a name="quote"></a></div>
</div>

<script type="text/javascript">
    var fileName = '6955668';
    var commentscount = 159;
    var islock = false
</script>
<script type="text/javascript" src="http://static.blog.csdn.net/scripts/comment.js"></script>
    <div id="ad_bot">
    </div>
<div id="report_dialog">
</div>

<div id="d-top"  style="bottom:60px;">
        <a id="quick-reply" class="btn btn-top q-reply" title="快速回复" style="display:none;">
            <img src="http://static.blog.csdn.net/images/blog-icon-reply.png" alt="快速回复">
        </a>    

    <a id="d-top-a" class="btn btn-top backtop"  style="display: none;" title="返回顶部" onclick="_gaq.push(['_trackEvent','function', 'onclick', 'blog_articles_huidaodingbu'])" style="">         
         <img src="http://static.blog.csdn.net/images/top.png" alt="TOP">
    </a>
</div>
<script type="text/javascript">
    $(function ()
    {
        $("#ad_frm_0").height("90px");
        
        setTimeout(function(){
            $("#ad_frm_2").height("200px");
        },1000);    
    });
  
</script>
<style type="text/css">
    .tag_list
    {
        background: none repeat scroll 0 0 #FFFFFF;
        border: 1px solid #D7CBC1;
        color: #000000;
        font-size: 12px;
        line-height: 20px;
        list-style: none outside none;
        margin: 10px 2% 0 1%;
        padding: 1px;
    }
    .tag_list h5
    {
        background: none repeat scroll 0 0 #E0DBD3;
        color: #47381C;
        font-size: 12px;
        height: 24px;
        line-height: 24px;
        padding: 0 5px;
        margin: 0;
    }
    .tag_list h5 a
    {
        color: #47381C;
    }
    .classify
    {
        margin: 10px 0;
        padding: 4px 12px 8px;
    }
    .classify a
    {
        margin-right: 20px;
        white-space: nowrap;
    }
</style>


<div class="tag_list" style="display:none"></div>
  <script  language="javascript" type='text/javascript'>     
      $(function(){
              setTimeout(function(){
                  $.get("/liuhe688/svc/GetTagContent",function(data){
                      $(".tag_list").html(data).show();
                  });     
              });
          },500);                       
 </script> 


<div id="pop_win" style="display:none ;position: absolute; z-index: 10000; border: 1px solid rgb(220, 220, 220); top: 222.5px; left: 630px; opacity: 1; background: none 0px 0px repeat scroll rgb(255, 255, 255);">
    
</div>
<div id="popup_mask"></div>
<style>
    #popup_mask
    {
        position: absolute;
        width: 100%;
        height: 100%;
        background: #000;
        z-index: 9999;
        left: 0px;
        top: 0px;
        opacity: 0.3;
        filter: alpha(opacity=30);
        display: none;
    }

</style>




<script type="text/javascript">
    $(function(){
        setTimeout(function(){
            $(".comment_body:contains('回复')").each(function(index,item){
                var u=$(this).text().split('：')[0].toString().replace("回复","")
                var thisComment=$(this);
                if(u)
                {
                    $.getJSON("https://passport.csdn.net/get/nick?callback=?", {users: u}, function(a) {
                        if(a!=null&&a.data!=null&&a.data.length>0)
                        {
                            nick=a.data[0].n; 
                            if(u!=nick)
                            {
                                thisComment.text(thisComment.text().replace(u,nick));  
                            }
                        }       
                    });  
                }
            });         

        },200);  

        setTimeout(function(){
            $(".math").each(function(index,value){$(this).find("span").last().css("color","#fff"); })
        },5000);

        setTimeout(function(){
            $(".math").each(function(index,value){$(this).find("span").last().css("color","#fff"); })
        },10000);

        setTimeout(function(){
            $(".math").each(function(index,value){$(this).find("span").last().css("color","#fff"); })
        },15000);
        
        setTimeout(function(){
            $("a img[src='http://js.tongji.linezing.com/stats.gif']").parent().css({"position":"absolute","left":"50%"});
        },300);
    });

    function loginbox(){
        var $logpop=$("#pop_win");
        $logpop.html('<iframe src="https://passport.csdn.net/account/loginbox?service=http://static.blog.csdn.net/callback.htm" frameborder="0" height="600" width="400" scrolling="no"></iframe>');

        $('#popup_mask').css({
            opacity: 0.5,
            width: $( document ).width() + 'px',
            height:  $( document ).height() + 'px'
        });
        $('#popup_mask').css("display","block");
 
        $logpop.css( {
            top: ($( window ).height() - $logpop.height())/ 2  + $( window 
       ).scrollTop() + 'px',
            left:($( window ).width() - $logpop.width())/ 2
        } );
 
        setTimeout( function () {
            $logpop.show();
            $logpop.css( {
                opacity: 1
            } );
        }, 200 );
 
        $('#popup_mask').unbind("click");
        $('#popup_mask').bind("click", function(){
            $('#popup_mask').hide();
            var $clopop = $("#pop_win");
            $("#common_ask_div_sc").css("display","none");
            $clopop.css( {
                opacity: 0
            } );
            setTimeout( function () {
                $clopop.hide();
            }, 350 );
            return false;
        });
    }   

</script>
 <script language="javascript" type="text/javascript" src="http://ads.csdn.net/js/async_new.js"></script>      


                        <div class="clear">
                        </div>
                    </div>                   
                
            </div>
                   
           <div id="side">
               
    <div class="side">
<div id="panel_Profile" class="panel">
<ul class="panel_head"><span>个人资料</span></ul>
<ul class="panel_body profile">
<div id="blog_userface">
    <a href="http://my.csdn.net/liuhe688" target="_blank">
    <img src="http://avatar.csdn.net/8/5/D/1_liuhe688.jpg" title="访问我的空间" style="max-width:90%"/>
    </a>
    <br />
    <span><a href="http://my.csdn.net/liuhe688" class="user_name" target="_blank">liuhe688</a></span>
</div>
<div class="interact">

    <a href="javascript:void(0);" class="attent" id="span_add_follow" title="[加关注]"></a>

 <a href="javascript:void(0);" class="letter"  title="[发私信]" onclick="window.open('http://msg.csdn.net/letters/model?receiver=liuhe688','_blank','height=350,width=700');_gaq.push(['_trackEvent','function', 'onclick', 'blog_articles_sixin'])"></a>  
</div>
<div id="blog_medal">
                <div id="bms_box">
                                            <a  target="_blank">
                                                    <img src="http://c.csdnimg.cn/jifen/images/xunzhang/xunzhang/chizhiyiheng.png" onmouseover="m_over_m(this,4)" onmouseout="m_out_m()" alt="1" >
                                            </a>
               </div>
</div>
<ul id="blog_rank">
    <li>访问：<span>2604371次</span></li>
    <li>积分：<span>9982</span> </li>    
    <li >等级： <span style="position:relative;display:inline-block;z-index:1" >
            <img src="http://c.csdnimg.cn/jifen/images/xunzhang/jianzhang/blog6.png" alt="" style="vertical-align: middle;" id="leveImg">
            <div id="smallTittle" style=" position: absolute;  left: -24px;  top: 25px;  text-align: center;  width: 101px;  height: 32px;  background-color: #fff;  line-height: 32px;  border: 2px #DDDDDD solid;  box-shadow: 0px 2px 2px rgba (0,0,0,0.1);  display: none;   z-index: 999;">
            <div style="left: 42%;  top: -8px;  position: absolute;  width: 0;  height: 0;  border-left: 10px solid transparent;  border-right: 10px solid transparent;  border-bottom: 8px solid #EAEAEA;"></div>
            积分：9982 </div>
        </span>  </li>
    <li>排名：<span>第1279名</span></li>
</ul>
<ul id="blog_statistics">
    <li>原创：<span>88篇</span></li>
    <li>转载：<span>0篇</span></li>
    <li>译文：<span>1篇</span></li>
    <li>评论：<span>1541条</span></li>
</ul>
</ul>
</div>




<div id="custom_column_21632947" class="panel">
<ul class="panel_head"><span>友情提示</span></ul>
<ul class="panel_body">

博客中的Android系列文章，是在2011年左右完成的，由于近年来Android版本变化等因素，结果可能会有些出入，请大家结合实际情况作参考。

</ul>
</div><div id="panel_Category" class="panel">
<ul class="panel_head"><span>文章分类</span></ul>
<ul class="panel_body">    
                 <li>
                    <a href="/liuhe688/article/category/616910" onclick="_gaq.push(['_trackEvent','function', 'onclick', 'blog_articles_wenzhangfenlei']); ">Front End</a><span>(27)</span>
                </li>
                 <li>
                    <a href="/liuhe688/article/category/790276" onclick="_gaq.push(['_trackEvent','function', 'onclick', 'blog_articles_wenzhangfenlei']); ">Android</a><span>(32)</span>
                </li>
                 <li>
                    <a href="/liuhe688/article/category/6284115" onclick="_gaq.push(['_trackEvent','function', 'onclick', 'blog_articles_wenzhangfenlei']); ">Algorithm</a><span>(17)</span>
                </li>
                 <li>
                    <a href="/liuhe688/article/category/655312" onclick="_gaq.push(['_trackEvent','function', 'onclick', 'blog_articles_wenzhangfenlei']); ">English</a><span>(6)</span>
                </li>
                 <li>
                    <a href="/liuhe688/article/category/616864" onclick="_gaq.push(['_trackEvent','function', 'onclick', 'blog_articles_wenzhangfenlei']); ">Java</a><span>(9)</span>
                </li>
                 <li>
                    <a href="/liuhe688/article/category/617628" onclick="_gaq.push(['_trackEvent','function', 'onclick', 'blog_articles_wenzhangfenlei']); ">Others</a><span>(4)</span>
                </li>
</ul>
</div><div id="hotarticls" class="panel">
<ul class="panel_head">
    <span>       
阅读排行    </span>
</ul>

<ul class="panel_body itemlist">
<li>
<a href="/liuhe688/article/details/6715983" title="Android中SQLite应用详解">Android中SQLite应用详解</a><span>(377440)</span>
</li>
<li>
<a href="/liuhe688/article/details/6733407" title="基础总结篇之一：Activity生命周期">基础总结篇之一：Activity生命周期</a><span>(376919)</span>
</li>
<li>
<a href="/liuhe688/article/details/6532519" title="详解Android中AsyncTask的使用">详解Android中AsyncTask的使用</a><span>(247920)</span>
</li>
<li>
<a href="/liuhe688/article/details/6415593" title="Android中解析XML">Android中解析XML</a><span>(176963)</span>
</li>
<li>
<a href="/liuhe688/article/details/6754323" title="基础总结篇之二：Activity的四种launchMode">基础总结篇之二：Activity的四种launchMode</a><span>(173417)</span>
</li>
<li>
<a href="/liuhe688/article/details/6955668" title="基础总结篇之五：BroadcastReceiver应用详解">基础总结篇之五：BroadcastReceiver应用详解</a><span>(168313)</span>
</li>
<li>
<a href="/liuhe688/article/details/6584143" title="Android中处理崩溃异常">Android中处理崩溃异常</a><span>(90313)</span>
</li>
<li>
<a href="/liuhe688/article/details/6712782" title="SQLite数据库简介">SQLite数据库简介</a><span>(72601)</span>
</li>
<li>
<a href="/liuhe688/article/details/6852523" title="Android中ListView分页加载数据">Android中ListView分页加载数据</a><span>(61564)</span>
</li>
<li>
<a href="/liuhe688/article/details/6425225" title="Android中使用HTTP服务">Android中使用HTTP服务</a><span>(60881)</span>
</li>
</ul>
</div>
<div id="hotarticls2" class="panel">
<ul class="panel_head"><span>评论排行</span></ul>
<ul class="panel_body itemlist">
<li>
<a href="/liuhe688/article/details/6715983" title="Android中SQLite应用详解">Android中SQLite应用详解</a><span>(192)</span>
</li>
<li>
<a href="/liuhe688/article/details/6754323" title="基础总结篇之二：Activity的四种launchMode">基础总结篇之二：Activity的四种launchMode</a><span>(161)</span>
</li>
<li>
<a href="/liuhe688/article/details/6955668" title="基础总结篇之五：BroadcastReceiver应用详解">基础总结篇之五：BroadcastReceiver应用详解</a><span>(159)</span>
</li>
<li>
<a href="/liuhe688/article/details/6532519" title="详解Android中AsyncTask的使用">详解Android中AsyncTask的使用</a><span>(134)</span>
</li>
<li>
<a href="/liuhe688/article/details/6584143" title="Android中处理崩溃异常">Android中处理崩溃异常</a><span>(112)</span>
</li>
<li>
<a href="/liuhe688/article/details/6733407" title="基础总结篇之一：Activity生命周期">基础总结篇之一：Activity生命周期</a><span>(100)</span>
</li>
<li>
<a href="/liuhe688/article/details/6415593" title="Android中解析XML">Android中解析XML</a><span>(92)</span>
</li>
<li>
<a href="/liuhe688/article/details/6407225" title="浅析Android中的消息机制">浅析Android中的消息机制</a><span>(58)</span>
</li>
<li>
<a href="/liuhe688/article/details/6679879" title="使用ANT打包Android应用">使用ANT打包Android应用</a><span>(39)</span>
</li>
<li>
<a href="/liuhe688/article/details/6761337" title="基础总结篇之三：Activity的task相关">基础总结篇之三：Activity的task相关</a><span>(38)</span>
</li>
</ul>
</div>
<div id="homepageArticles" class="panel tracking-ad" data-mod="popu_4">
<ul class="panel_head"><span>推荐文章</span></ul>
<ul class="panel_body" id="ad_commend">
<ul>
<li><a href="http://blog.csdn.net/yyh352091626/article/details/53304728" target="_blank">* RxJava详解，由浅入深</a></li>
<li><a href="http://blog.csdn.net/foruok/article/details/53063089" 
target="_blank">* 倍升工作效率的小策略</a></li>
<li><a href="http://blog.csdn.net/jiangwei0910410003/article/details/53099390" 
target="_blank">* Android热修复框架AndFix原理解析及使用</a></li>
<li><a href="http://blog.csdn.net/xiangzhihong8/article/details/53312278" target="_blank">* “区块链”究竟是什么鬼</a></li>
<li><a href="http://blog.csdn.net/yinwenjie/article/details/53033017" 
target="_blank">* 架构设计：系统存储-MySQL主从方案业务连接透明化（中）</a></li>


</ul></ul>
</div>


<div id="newcomments" class="panel">
<ul class="panel_head"><span>最新评论</span></ul>
<ul class="panel_body itemlist">
    <li>
   
         <a href="/liuhe688/article/details/6955668#comments">基础总结篇之五：BroadcastReceiver应用详解</a>
    <p style="margin:0px;"><a href="/u013396522" class="user_name">u013396522</a>:
关注您很久了大神，是否考虑过出一本android方面的书籍？必定大卖
    </p>
    </li>
    <li>
   
         <a href="/liuhe688/article/details/6955668#comments">基础总结篇之五：BroadcastReceiver应用详解</a>
    <p style="margin:0px;"><a href="/u013396522" class="user_name">u013396522</a>:
关注您很久了大神，是否考虑过出一本android方面的书籍？必定大卖
    </p>
    </li>
    <li>
   
         <a href="/liuhe688/article/details/6733407#comments">基础总结篇之一：Activity生命周期</a>
    <p style="margin:0px;"><a href="/liyong8525" class="user_name">liyong8525</a>:
感谢博主，最近在尝试做一个音乐播放器，确实缺少这方面的知识，太好了
    </p>
    </li>
    <li>
   
         <a href="/liuhe688/article/details/6532519#comments">详解Android中AsyncTask的使用</a>
    <p style="margin:0px;"><a href="/qq_29338775" class="user_name">qq_29338775</a>:
大概看了一下 一看评论  我决定重新看....
    </p>
    </li>
    <li>
   
         <a href="/liuhe688/article/details/6532519#comments">基础总结篇之一：Activity生命周期</a>
    <p style="margin:0px;"><a href="/linhingf" class="user_name">linhingf</a>:
onWindowFocusChanged 方法对内存消耗会不会比较多？
    </p>
    </li>
    <li>
   
         <a href="/liuhe688/article/details/6754323#comments">基础总结篇之二：Activity的四种launchMode</a>
    <p style="margin:0px;"><a href="/gjwqh" class="user_name">gjwqh</a>:
@qq_25778369:现在知道怎么转了吧?sb
    </p>
    </li>
    <li>
   
         <a href="/liuhe688/article/details/6754323#comments">详解Android中AsyncTask的使用</a>
    <p style="margin:0px;"><a href="/a_100861" class="user_name">a_100861</a>:
都是五年前的博客，质量还相当高啊
    </p>
    </li>
    <li>
   
         <a href="/liuhe688/article/details/6754323#comments">基础总结篇之二：Activity的四种launchMode</a>
    <p style="margin:0px;"><a href="/inofance" class="user_name">inofance</a>:
讲的很清楚，图示一看就能懂
    </p>
    </li>
    <li>
   
         <a href="/liuhe688/article/details/6754323#comments">详解Android中AsyncTask的使用</a>
    <p style="margin:0px;"><a href="/qq_33781378" class="user_name">qq_33781378</a>:
特地登陆感谢楼主。写的很详细，通俗易懂。赞赞赞
    </p>
    </li>
    <li>
   
         <a href="/liuhe688/article/details/51452892#comments">React入门实践之Todo List</a>
    <p style="margin:0px;"><a href="/qq_36356142" class="user_name">qq_36356142</a>:
看了您的感觉收获挺大的，只是不知道如何通过点击事件向mockData 中添加数据呢？
    </p>
    </li>
</ul>
</div>
    </div>
    <div class="clear">
    </div>


                 <!-- 广告位开始 --> 
                    <ins data-revive-zoneid="189" data-revive-id="8c38e720de1c90a6f6ff52f3f89c4d57"></ins> 
                <!-- 广告位结束 -->

           </div>   

            <div class="clear">
            </div>
        </div>

        

<script type="text/javascript" src="http://c.csdnimg.cn/rabbit/cnick/cnick.js"></script>
<script type="text/javascript" src="http://static.blog.csdn.net/scripts/newblog.min.js"></script>


<script type="text/javascript" src="http://medal.blog.csdn.net/showblogmedal.ashx?blogid=543879"></script>
<script type="text/javascript" src="http://static.blog.csdn.net/scripts/JavaScript1.js"></script>

    <script type="text/javascript" src="http://passport.csdn.net/content/loginbox/login.js"></script>
<script type="text/javascript">document.write("<img src=http://counter.csdn.net/pv.aspx?id=24 border=0 width=0 height=0>");</script>
<script type="text/javascript" src="http://www.csdn.net/ui/scripts/Csdn/counter.js"></script>
<script type="text/javascript" src="http://ad.csdn.net/scripts/ad-blog.js"></script>
<script type="text/javascript">
    $(function () {
        function __get_code_toolbar(snippet_id) {
            return $("<span class='tracking-ad' data-mod='popu_167'><a href='https://code.csdn.net/snippets/"
                    + snippet_id
                    + "' target='_blank' title='在CODE上查看代码片'  style='text-indent:0;'><img src='https://code.csdn.net/assets/CODE_ico.png' width=12 height=12 alt='在CODE上查看代码片' style='position:relative;top:1px;left:2px;'/></a></span>"
                    + "<span class='tracking-ad' data-mod='popu_170'><a href='https://code.csdn.net/snippets/"
                    + snippet_id
                    + "/fork' target='_blank' title='派生到我的代码片' style='text-indent:0;'><img src='https://code.csdn.net/assets/ico_fork.svg' width=12 height=12 alt='派生到我的代码片' style='position:relative;top:2px;left:2px;'/></a></span>");
        }
        
        $("[code_snippet_id]").each(function () {
            __s_id = $(this).attr("code_snippet_id");
            if (__s_id != null && __s_id != "" && __s_id != 0 && parseInt(__s_id) > 70020) {
                __code_tool = __get_code_toolbar(__s_id);
                $(this).prev().find(".tools").append(__code_tool);
            }
        });

        $(".bar").show();
    });
</script>





    </div>
      <!--new top-->
    
    <script id="csdn-toolbar-id" btnId="header_notice_num" wrapId="note1" count="5" subCount="5" type="text/javascript" src="http://c.csdnimg.cn/public/common/toolbar/js/toolbar.js"></script>     <!--new top-->
   
    <link href="http://c.csdnimg.cn/comm_ask/css/ask_float_block.css" type="text/css" rel="stylesheet" />
    <script language='JavaScript' type='text/javascript' src='http://c.csdnimg.cn/comm_ask/js/libs/wmd.js'></script>
    <script language='JavaScript' type='text/javascript' src='http://c.csdnimg.cn/comm_ask/js/libs/showdown.js'></script>
    <script language='JavaScript' type='text/javascript' src='http://c.csdnimg.cn/comm_ask/js/libs/prettify.js'></script>
    <script language='JavaScript' type='text/javascript' src='http://c.csdnimg.cn/comm_ask/js/apps/ask_float_block.js'></script>
   

  <div id="a52b5334d" style="width: 1px; height: 1px; display: none;">
                    <script id="adJs52b5334"></script>
                    <script>document.getElementById("adJs52b5334").src = "http://ads.csdn.net/js/opt/52b5334.js?t=" + Math.random();</script>
   </div>

    <link rel="stylesheet" href="http://static.blog.csdn.net/css/blog_code.css" />
    <script type="text/javascript" src="http://static.blog.csdn.net/scripts/saveToCode.js"></script>
      <script type="text/javascript" src="//csdnimg.cn/rabbit/tracking-ad/main.js?75eacd8"></script>
</body>
</html>   
 